package com.example.springbootdemo.es7;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.xcontent.XContentType;

public class ElasticsearchOfDoc {
    // 新增文档, 规则 put /user/_doc/1001
    public static final ElasticsearchTask insertDoc = client -> {
        // 设置索引及唯一性标识
        IndexRequest request = new IndexRequest("user");
        request.id("1001");
        request.timeout(TimeValue.timeValueSeconds(1));
        // 创建数据对象
        User user = new User();
        user.setName("zhangsan");
        user.setAge(30);
        user.setSex("男");
        ObjectMapper objectMapper = new ObjectMapper();
        String productJson = objectMapper.writeValueAsString(user);
        // 添加文档数据，数据格式为 JSON 格式
        request.source(productJson, XContentType.JSON);
        // 客户端发送请求，获取响应对象
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("IndexRequest Result: " + response);
    };
    // 查询文档
    public static final ElasticsearchTask getDoc = client -> {
        GetRequest request = new GetRequest().index("user").id("1001");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        System.out.println("GetRequest Result: " + response);
    };
    // 修改文档
    public static final ElasticsearchTask updateDoc = client -> {
        UpdateRequest request = new UpdateRequest();
        request.index("user").id("1001");
        request.timeout("1s");
//        request.doc(XContentType.JSON, "sex", "女");
        User user = new User();
        user.setAge(31);
        user.setSex("女");
        ObjectMapper objectMapper = new ObjectMapper();
        String productJson = objectMapper.writeValueAsString(user);
        request.doc(productJson, XContentType.JSON);
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        System.out.println("UpdateRequest Result: " + response);
    };
    // 删除文档
    public static final ElasticsearchTask deleteDoc = client -> {
        DeleteRequest request = new DeleteRequest().index("user").id("1001");
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        System.out.println("DeleteRequest Result: " + response.toString());
    };
    // 主程序
    public static void main(String[] args) throws JsonProcessingException {
        ConnectElasticsearch.connect(insertDoc, getDoc, updateDoc, getDoc, deleteDoc);
    }
}
